package com.jcl.recruit.wechat.mapper;

import com.jcl.recruit.wechat.model.WeixinConfig;
import org.apache.ibatis.annotations.*;

public interface WeixinConfigMapper {

    @Select({
            "<script>",
            "select * from weixin_config ",
            "<where>",
            "<if test=\"appId!=null\"> app_id = #{appId}</if>",
            "<if test=\"appSecret!=null\"> and app_secret = #{appSecret}</if>",
            "</where>",
            "</script>"
    })
    WeixinConfig findOne(@Param("appId") String appId, @Param("appSecret") String appSecret);

    @InsertProvider(type = WeixinConfigProvider.class, method = "insertProvider")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(WeixinConfig config);

    @UpdateProvider(type = WeixinConfigProvider.class, method = "updateProvider")
    int update(WeixinConfig config);

    @Update({
            "update weixin_config set access_token = #{accessToken}, token_over_time = #{tokenOverTime}, " +
                    " token_update_time = now() where app_id = #{appId}"
    })
    int updateToken(@Param("appId") String appId, @Param("accessToken") String accessToken,
                    @Param("tokenOverTime") Long tokenOverTime);

    @Update({
            "update weixin_config set ticket = #{ticket}, ticket_over_time = #{ticketOverTime}, " +
                    " ticket_update_time = now() where app_id = #{appId}"
    })
    int updateTicket(@Param("appId") String appId, @Param("ticket") String ticket,
                     @Param("ticketOverTime") Long ticketOverTime);


}
